Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mapContextAsync #125

Merged
merged 5 commits into from
Jan 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"presets": [
"stage-3"
],
"plugins": [
"babel-plugin-transform-es2015-modules-commonjs"
]
Expand Down
14 changes: 14 additions & 0 deletions bundle_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import acornAsyncIteration from 'acorn-async-iteration/inject';

export default {
output: {
format: 'umd'
},
acorn: {
ecmaVersion: 9,
plugins: { asyncIteration: true }
},
acornInjectPlugins: [
acornAsyncIteration
]
};
4 changes: 3 additions & 1 deletion common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ test:
ifneq (,$(wildcard ./test/__setup.js))
@mocha --recursive --ui tdd \
--require babel-register \
--require babel-polyfill \
--require ./test/__setup
else
@mocha --recursive --ui tdd \
--require babel-register
--require babel-register \
--require babel-polyfill
endif

html: $(SOURCES)
Expand Down
4 changes: 4 additions & 0 deletions compat_config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import babel from 'rollup-plugin-babel';

export default {
output: {
format: 'umd'
},
plugins: [
babel({
'babelrc': false,
'presets': [
'stage-3',
['env', {
// Cf. https://github.com/rollup/rollup-plugin-babel#modules
'modules': false,
Expand Down
3 changes: 2 additions & 1 deletion eslint_src.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"parser": "babel-eslint",
"parserOptions": {
"ecmaVersion": 8,
"ecmaVersion": 9,
"sourceType": "module"
},
"env": {
Expand Down
3 changes: 2 additions & 1 deletion eslint_test.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"parser": "babel-eslint",
"parserOptions": {
"ecmaVersion": 8,
"ecmaVersion": 9,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
Expand Down
3 changes: 1 addition & 2 deletions fluent-dom/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ build: $(PACKAGE).js compat.js

$(PACKAGE).js: $(SOURCES)
@rollup $(CURDIR)/src/index.js \
--output.format umd \
--config $(ROOT)/bundle_config.js \
--banner "/* $(PACKAGE)@$(VERSION) */" \
--amd.id $(PACKAGE) \
--name $(GLOBAL) \
Expand All @@ -17,7 +17,6 @@ $(PACKAGE).js: $(SOURCES)
compat.js: $(SOURCES)
@rollup $(CURDIR)/src/index.js \
--config $(ROOT)/compat_config.js \
--output.format umd \
--banner "/* $(PACKAGE)@$(VERSION) */" \
--amd.id $(PACKAGE) \
--name $(GLOBAL) \
Expand Down
6 changes: 4 additions & 2 deletions fluent-gecko/xpcom_config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import bundleConfig from '../bundle_config';

const version = require('../fluent/package.json').version;

export default {
export default Object.assign({}, bundleConfig, {
preferConst: true,
context: 'this',
output: {
Expand All @@ -23,4 +25,4 @@ export default {
*/\n\n`,
intro: `/* fluent@${version} */`,
}
};
});
5 changes: 3 additions & 2 deletions fluent-gecko/xpcom_dom_config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { resolve } from 'path';
import bundleConfig from '../bundle_config';

const version = require('../fluent/package.json').version;

export default {
export default Object.assign({}, bundleConfig, {
external: [
resolve('../fluent-dom/src/localization.js')
],
Expand All @@ -28,4 +29,4 @@ export default {
},
preferConst: true,
context: 'this'
};
});
5 changes: 3 additions & 2 deletions fluent-intl-polyfill/bundle_config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import bundleConfig from '../bundle_config';
import nodeResolve from 'rollup-plugin-node-resolve';

export default {
export default Object.assign({}, bundleConfig, {
context: 'this',
plugins: [
nodeResolve(),
]
};
});

20 changes: 4 additions & 16 deletions fluent-intl-polyfill/compat_config.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
import compatConfig from '../compat_config';
import nodeResolve from 'rollup-plugin-node-resolve';
import babel from 'rollup-plugin-babel';

export default {
export default Object.assign({}, compatConfig, {
context: 'this',
plugins: [
nodeResolve(),
babel({
'babelrc': false,
'presets': [
['env', {
'modules': false
}]
],
'plugins': [
'external-helpers',
['babel-plugin-transform-builtin-extend', {
globals: ['Error']
}]
]
}),
...compatConfig.plugins
],
};
});
4 changes: 1 addition & 3 deletions fluent-intl-polyfill/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@ build: $(PACKAGE).js compat.js

$(PACKAGE).js: $(SOURCES)
@rollup $(CURDIR)/src/index.js \
--output.format umd \
--config $(CURDIR)/bundle_config.js \
--banner "/* $(PACKAGE)@$(VERSION) */" \
--amd.id $(PACKAGE) \
--name $(GLOBAL) \
--config $(CURDIR)/bundle_config.js \
--output.file $@
@echo -e " $(OK) $@ built"

compat.js: $(SOURCES)
@rollup $(CURDIR)/src/index.js \
--config $(CURDIR)/compat_config.js \
--output.format umd \
--banner "/* $(PACKAGE)@$(VERSION) */" \
--amd.id $(PACKAGE) \
--name $(GLOBAL) \
Expand Down
3 changes: 1 addition & 2 deletions fluent-langneg/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ build: $(PACKAGE).js compat.js

$(PACKAGE).js: $(SOURCES)
@rollup $(CURDIR)/src/index.js \
--output.format umd \
--config $(ROOT)/bundle_config.js \
--banner "/* $(PACKAGE)@$(VERSION) */" \
--amd.id $(PACKAGE) \
--name $(GLOBAL) \
Expand All @@ -17,7 +17,6 @@ $(PACKAGE).js: $(SOURCES)
compat.js: $(SOURCES)
@rollup $(CURDIR)/src/index.js \
--config $(ROOT)/compat_config.js \
--output.format umd \
--banner "/* $(PACKAGE)@$(VERSION) */" \
--amd.id $(PACKAGE) \
--name $(GLOBAL) \
Expand Down
3 changes: 1 addition & 2 deletions fluent-react/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ build: $(PACKAGE).js compat.js

$(PACKAGE).js: $(SOURCES)
@rollup $(CURDIR)/src/index.js \
--output.format umd \
--config $(ROOT)/bundle_config.js \
--banner "/* $(PACKAGE)@$(VERSION) */" \
--amd.id $(PACKAGE) \
--name $(GLOBAL) \
Expand All @@ -19,7 +19,6 @@ $(PACKAGE).js: $(SOURCES)
compat.js: $(SOURCES)
@rollup $(CURDIR)/src/index.js \
--config $(ROOT)/compat_config.js \
--output.format umd \
--banner "/* $(PACKAGE)@$(VERSION) */" \
--amd.id $(PACKAGE) \
--name $(GLOBAL) \
Expand Down
3 changes: 1 addition & 2 deletions fluent-syntax/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ build: $(PACKAGE).js compat.js

$(PACKAGE).js: $(SOURCES)
@rollup $(CURDIR)/src/index.js \
--output.format umd \
--config $(ROOT)/bundle_config.js \
--banner "/* $(PACKAGE)@$(VERSION) */" \
--amd.id $(PACKAGE) \
--name $(GLOBAL) \
Expand All @@ -17,7 +17,6 @@ $(PACKAGE).js: $(SOURCES)
compat.js: $(SOURCES)
@rollup $(CURDIR)/src/index.js \
--config $(ROOT)/compat_config.js \
--output.format umd \
--banner "/* $(PACKAGE)@$(VERSION) */" \
--amd.id $(PACKAGE) \
--name $(GLOBAL) \
Expand Down
8 changes: 6 additions & 2 deletions fluent-web/bundle_config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import bundleConfig from '../bundle_config';
import nodeResolve from 'rollup-plugin-node-resolve';

export default {
export default Object.assign({}, bundleConfig, {
output: {
format: 'iife'
},
context: 'this',
plugins: [
nodeResolve(),
]
};
});
20 changes: 4 additions & 16 deletions fluent-web/compat_config.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
import compatConfig from '../compat_config';
import nodeResolve from 'rollup-plugin-node-resolve';
import babel from 'rollup-plugin-babel';

export default {
export default Object.assign({}, compatConfig, {
context: 'this',
plugins: [
nodeResolve(),
babel({
'babelrc': false,
'presets': [
['env', {
'modules': false
}]
],
'plugins': [
'external-helpers',
['babel-plugin-transform-builtin-extend', {
globals: ['Error']
}]
]
}),
...compatConfig.plugins
],
};
});
4 changes: 1 addition & 3 deletions fluent-web/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@ build: $(PACKAGE).js compat.js

$(PACKAGE).js: $(SOURCES)
@rollup $(CURDIR)/src/index.js \
--output.format iife \
--config $(CURDIR)/bundle_config.js \
--banner "/* $(PACKAGE)@$(VERSION) */" \
--amd.id $(PACKAGE) \
--name $(GLOBAL) \
--config $(CURDIR)/bundle_config.js \
--output.file $@
@echo -e " $(OK) $@ built"

compat.js: $(SOURCES)
@rollup $(CURDIR)/src/index.js \
--config $(CURDIR)/compat_config.js \
--output.format umd \
--banner "/* $(PACKAGE)@$(VERSION) */" \
--amd.id $(PACKAGE) \
--name $(GLOBAL) \
Expand Down
3 changes: 1 addition & 2 deletions fluent/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ build: $(PACKAGE).js compat.js

$(PACKAGE).js: $(SOURCES)
@rollup $(CURDIR)/src/index.js \
--output.format umd \
--config $(ROOT)/bundle_config.js \
--banner "/* $(PACKAGE)@$(VERSION) */" \
--amd.id $(PACKAGE) \
--name $(GLOBAL) \
Expand All @@ -17,7 +17,6 @@ $(PACKAGE).js: $(SOURCES)
compat.js: $(SOURCES)
@rollup $(CURDIR)/src/index.js \
--config $(ROOT)/compat_config.js \
--output.format umd \
--banner "/* $(PACKAGE)@$(VERSION) */" \
--amd.id $(PACKAGE) \
--name $(GLOBAL) \
Expand Down
41 changes: 40 additions & 1 deletion fluent/src/fallback.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
* @param {string|Array<string>} ids
* @returns {MessageContext|Array<MessageContext>}
*/

export function mapContextSync(iterable, ids) {
if (!Array.isArray(ids)) {
return getContextForId(iterable, ids);
Expand All @@ -71,3 +70,43 @@ function getContextForId(iterable, id) {

return null;
}

/*
* Asynchronously map an identifier or an array of identifiers to the best
* `MessageContext` instance(s).
*
* @param {AsyncIterable} iterable
* @param {string|Array<string>} ids
* @returns {Promise<MessageContext|Array<MessageContext>>}
*/
export async function mapContextAsync(iterable, ids) {
if (!Array.isArray(ids)) {
for await (const context of iterable) {
if (context.hasMessage(ids)) {
return context;
}
}
}

let remainingCount = ids.length;
const foundContexts = new Array(remainingCount).fill(null);

for await (const context of iterable) {
// XXX Switch to const [index, id] of id.entries() when we move to Babel 7.
// See https://github.com/babel/babel/issues/5880.
for (let index = 0; index < ids.length; index++) {
const id = ids[index];
if (!foundContexts[index] && context.hasMessage(id)) {
foundContexts[index] = context;
remainingCount--;
}

// Return early when all ids have been mapped to contexts.
if (remainingCount === 0) {
return foundContexts;
}
}
}

return foundContexts;
}
2 changes: 1 addition & 1 deletion fluent/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ export {
} from './types';

export { default as CachedIterable } from './cached_iterable';
export { mapContextSync } from './fallback';
export { mapContextSync, mapContextAsync } from './fallback';
Loading