Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | 27x 1x 1x 26x 2x 24x 2x 24x 11x 7x 7x 2x 5x 5x 4x 4x 4x 4x 4x 4x 2x 1x 11x 123x | /**
* @prettier
*/
/*
* Part of Pleiar.no - a collection of tools for nurses
*
* Copyright (C) Fagforbundet 2020
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
// @flow
// eslint-disable-next-line flowtype/require-exact-type
export type asyncLoaderRoleType = {
_ALR_initializing: ?Promise<null>,
_ALR_hasInitialized: ?boolean,
_ALR_onInitialize: ?Array<() => mixed>,
onInitialize: (() => mixed) => mixed,
initialize: () => Promise<null>,
hasInitialized: () => boolean,
};
/**
* This is a "base" object that you can add to another object to add functionality
* to that object to asynchronously load some data.
*
* It expects the following methods, with these exact signatures, to be present
* in any object that consumes it:
*
* _performAsyncImport() - This should return the promise from the import call,
* like: import(/* webpackChunkName: "search-index", webpackPreload: true * /
* '../data/search-index.json').
*
* _loadedData(data) - this gets the data structure once it has been loaded.
*/
const asyncLoaderRole: asyncLoaderRoleType = {
_ALR_initializing: null,
_ALR_hasInitialized: false,
_ALR_onInitialize: null,
/**
* Must be overridden by consumer
*/
_performAsyncImport() {
throw "ERROR: Consuming object of asyncLoaderRole has not provided _performAsyncImport";
},
/**
* Must be overridden by consumer
*/
_loadedData() {
throw "ERROR: Consuming object of asyncLoaderRole has not provided _loadedData";
},
/**
* Subscribes to an "on-initialize" event. Each callback will be called
* when the initialize() promise resolves (but it won't trigger an
* initialize() call). If we have already been initialized, the function
* is executed immediately.
*/
onInitialize(func: () => mixed) {
// $FlowExpectedError[object-this-reference]
if (this.hasInitialized()) {
func();
} else {
// $FlowExpectedError[object-this-reference]
if (this._ALR_onInitialize === null) {
// $FlowExpectedError[object-this-reference]
this._ALR_onInitialize = [];
}
// $FlowExpectedError[object-this-reference]
this._ALR_onInitialize.push(func);
}
},
/**
* Initializes this instance of our consuming object. Returns a promise that gets
* resolved when the object has been initialized
*/
initialize(): Promise<null> {
// $FlowExpectedError[object-this-reference]
if (this._ALR_initializing === null) {
// $FlowExpectedError[object-this-reference]
this._ALR_initializing = new Promise((resolve, reject) => {
// $FlowExpectedError[object-this-reference]
if (this.hasInitialized()) {
resolve(null);
} else {
// $FlowExpectedError[object-this-reference]
const parentPromise = this._performAsyncImport();
parentPromise
.then((module) => {
// $FlowExpectedError[object-this-reference]
this._loadedData(module.default);
// $FlowExpectedError[object-this-reference]
this._ALR_hasInitialized = true;
// $FlowExpectedError[object-this-reference]
const onInitialize = this._ALR_onInitialize;
// $FlowExpectedError[object-this-reference]
this._ALR_onInitialize = [];
resolve(null);
for (const func of onInitialize) {
func();
}
})
.catch((error) => {
reject(
"search-index.json failed to load: " + error,
);
});
}
});
}
// $FlowExpectedError[object-this-reference]
return this._ALR_initializing;
},
/**
* This returns a boolean, true if PleiarSearcher has been initialized,
* false otherwise.
*/
hasInitialized(): boolean {
// $FlowExpectedError[object-this-reference]
return this._ALR_hasInitialized;
},
};
export { asyncLoaderRole };
|