mirror of
https://github.com/keymanapp/keyman.git
synced 2026-10-12 17:07:31 +00:00
Fixes #6320. Part 1 of moving from lerna to a simpler, maintained monorepo solution, using TypeScript Projects and NPM Workspaces. There is more work to be done here. At this point, KeymanWeb builds and runs without errors, but the built file is substantially different, mostly in include order. Using TypeScript Projects, we move away from the need to run build scripts in various locations for almost all the Typescript modules. TODO: Embedded versions and tests have not been verified. TODO: developer/server is not yet verified. TODO: developer/js (needs a rename!) is not yet verified. TODO: Currently, the predictive-text folder needs refactoring to move the construction of the worker wrapper out of the Predictive Text build and into the final assembly of keymanweb.js (as it should be valid to run it as a separate .js anyway). TODO: Most of the `<reference>` paths need to be re-verified. Ideally there should be no references outside the current module for any given .ts. TODO: The embedded vs browser vs node (headless) builds should be tidied up for consistency so that it's obvious what depends on what. This is currently messiest in the predictive-text folder, where the output names diverge from the filenames and the various files are mixed in the same folder (as evidenced by the exclusions listed in each tsconfig.json). TODO: `npm install` should be removed from most build scripts and instead `npm ci` (#6196) should be run only once from the top-level folder for any given build. I've had eliminated side-effects from the `install` action for npm, which makes it easier to reason about state. TODO: verify_npm_setup and related functions can probably be eliminated. TODO: most of the build scripts should be largely eliminated for web. TODO: several ts projects use inconsistent output folders. TODO: it may be possible to generate a .d.ts for models/types so that we can use a consistent reference for those as well. TODO: build.sh, tsconfig.json should always be in the module's top-level folder, not in a subfolder such as src (e.g. see input-processor/src, keyboard-processor/src, web/source). TODO: resources/web-environment should be in common/web. TODO: other js node_modules imports should be wrapped like es6-shim. TODO: fix up the publish code for npm modules TODO: eliminate version numbers from package.json if possible? Whew, that's most of the stuff I noticed!
90 lines
No EOL
3.3 KiB
TypeScript
90 lines
No EOL
3.3 KiB
TypeScript
/*
|
|
* Copyright (c) 2018 National Research Council Canada (author: Eddie A. Santos)
|
|
* Copyright (c) 2018 SIL International
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
* this software and associated documentation files (the "Software"), to deal in
|
|
* the Software without restriction, including without limitation the rights to
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
* subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
*/
|
|
|
|
|
|
type Resolve<T> = (value?: T | PromiseLike<T>) => void;
|
|
type Reject = (reason?: any) => void;
|
|
interface PromiseCallbacks<T> {
|
|
resolve: Resolve<T>;
|
|
reject: Reject;
|
|
}
|
|
|
|
namespace com.keyman.text.prediction {
|
|
/**
|
|
* Associate tokens with promises.
|
|
*
|
|
* First, .make() a promise -- associate a token with resolve/reject callbacks.
|
|
*
|
|
* You can either .keep() a promise -- resolve() and forget it;
|
|
* Or you may also .break() a promise -- reject() and forget it.
|
|
*
|
|
* <T> is the type of resolved value (value yielded successfully by promise).
|
|
*/
|
|
export class PromiseStore<T> {
|
|
// IE11 offers partial support for new Map().
|
|
// Assume only .get(), .set(), .has(), .delete(), and .size work.
|
|
// See: http://kangax.github.io/compat-table/es6/#test-Map
|
|
private _promises: Map<Token, PromiseCallbacks<T>>;
|
|
constructor() {
|
|
this._promises = new Map();
|
|
}
|
|
/**
|
|
* How many promises are currently being tracked?
|
|
*/
|
|
get length(): number {
|
|
return this._promises.size;
|
|
}
|
|
/**
|
|
* Associate a token with its respective resolve and reject callbacks.
|
|
*/
|
|
make(token: Token, resolve: Resolve<T>, reject: Reject): void {
|
|
if (this._promises.has(token)) {
|
|
return reject(`Existing request with token ${token}`);
|
|
}
|
|
this._promises.set(token, { reject, resolve });
|
|
}
|
|
/**
|
|
* Resolve the promise associated with a token (with a value!).
|
|
* Once the promise is resolved, the token is removed..
|
|
*/
|
|
keep(token: Token, value: T) {
|
|
let callbacks = this._promises.get(token);
|
|
if (!callbacks) {
|
|
throw new Error(`No promise associated with token: ${token}`);
|
|
}
|
|
let accept = callbacks.resolve;
|
|
this._promises.delete(token);
|
|
return accept(value);
|
|
}
|
|
/**
|
|
* Instantly reject and forget a promise associated with the token.
|
|
*/
|
|
break(token: Token, reason?: any): void {
|
|
let callbacks = this._promises.get(token);
|
|
if (!callbacks) {
|
|
throw new Error(`No promise associated with token: ${token}`);
|
|
}
|
|
this._promises.delete(token);
|
|
callbacks.reject(reason);
|
|
}
|
|
}
|
|
} |