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

Extensibility proof-of-concept #15

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion ember-polaris-service/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { factory } from './factory.ts';
export { lookup, override } from './primitives.ts';
export { lookup, provide, override } from './primitives.ts';
export {
type ServiceFactory,
type ServiceManager,
Expand Down
3 changes: 3 additions & 0 deletions ember-polaris-service/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export interface ServiceFactory<T> {
[INSTANTIATE]: (scope: Scope) => T;
}

export type ServiceInstanceType<T extends ServiceFactory<unknown>> =
T extends ServiceFactory<infer Instance> ? Instance : unknown;

export interface ServiceManager<D extends object, T> {
createService(definition: D): T;
}
Expand Down
16 changes: 15 additions & 1 deletion ember-polaris-service/src/primitives.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { assert, runInDebug } from '@ember/debug';
import {
type ServiceFactory,
type ServiceInstanceType,
instantiate,
isServiceFactory,
} from './manager.ts';
Expand All @@ -24,6 +25,14 @@ export function lookup<T>(scope: Scope, factory: ServiceFactory<T>): T {
return service;
}

export function provide<T1 extends ServiceFactory<unknown>, T2 extends T1>(
factory: T1,
provider: T2,
): ServiceFactory<ServiceInstanceType<T2>> {
Providers.set(factory, provider);
return factory as ServiceFactory<ServiceInstanceType<T2>>;
}

export function override<T>(
scope: Scope,
factory: ServiceFactory<T>,
Expand Down Expand Up @@ -70,7 +79,10 @@ function factoryFor<T>(
factory: ServiceFactory<T>,
): ServiceFactory<T> {
const overrides = mapFor(scope, Overrides);
return (overrides.get(factory) ?? factory) as ServiceFactory<T>;

return (overrides.get(factory) ??
Providers.get(factory) ??
factory) as ServiceFactory<T>;
}

type InstantiatedServices = WeakMap<ServiceFactory<unknown>, unknown>;
Expand All @@ -82,4 +94,6 @@ type OverriddenServices = WeakMap<

const Services = new WeakMap<Scope, InstantiatedServices>();

const Providers: OverriddenServices = new WeakMap();

const Overrides = new WeakMap<Scope, OverriddenServices>();
9 changes: 9 additions & 0 deletions extensibility/ember-storage-batch/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# unconventional js
/blueprints/*/files/

# compiled output
/dist/
/declarations/

# misc
/coverage/
50 changes: 50 additions & 0 deletions extensibility/ember-storage-batch/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict';

module.exports = {
root: true,
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 'latest',
},
plugins: ['ember'],
extends: [
'eslint:recommended',
'plugin:ember/recommended',
'plugin:prettier/recommended',
],
env: {
browser: true,
},
rules: {},
overrides: [
// ts files
{
files: ['**/*.ts', '**/*.gts'],
extends: [
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
],
rules: {
// Add any custom rules here
},
},
// node files
{
files: [
'./.eslintrc.cjs',
'./.prettierrc.cjs',
'./.template-lintrc.cjs',
'./addon-main.cjs',
],
parserOptions: {
sourceType: 'script',
},
env: {
browser: false,
node: true,
},
plugins: ['n'],
extends: ['plugin:n/recommended'],
},
],
};
6 changes: 6 additions & 0 deletions extensibility/ember-storage-batch/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# compiled output
/dist
/declarations

# npm/pnpm/yarn pack output
*.tgz
9 changes: 9 additions & 0 deletions extensibility/ember-storage-batch/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# unconventional js
/blueprints/*/files/

# compiled output
/dist/
/declarations/

# misc
/coverage/
14 changes: 14 additions & 0 deletions extensibility/ember-storage-batch/.prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

module.exports = {
plugins: ['prettier-plugin-ember-template-tag'],
singleQuote: true,
overrides: [
{
files: '*.{gjs,gts}',
options: {
parser: 'ember-template-tag',
},
},
],
};
5 changes: 5 additions & 0 deletions extensibility/ember-storage-batch/.template-lintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = {
extends: 'recommended',
};
9 changes: 9 additions & 0 deletions extensibility/ember-storage-batch/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2023

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.
3 changes: 3 additions & 0 deletions extensibility/ember-storage-batch/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# ember-storage-batch

An extension for ember-storage that adds batch operations.
4 changes: 4 additions & 0 deletions extensibility/ember-storage-batch/addon-main.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict';

const { addonV1Shim } = require('@embroider/addon-shim');
module.exports = addonV1Shim(__dirname);
13 changes: 13 additions & 0 deletions extensibility/ember-storage-batch/babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"plugins": [
["@babel/plugin-transform-typescript", { "allExtensions": true, "onlyRemoveTypeImports": true, "allowDeclareFields": true }],
"@embroider/addon-dev/template-colocation-plugin",
"@babel/plugin-transform-class-static-block",
["babel-plugin-ember-template-compilation", {
"targetFormat": "hbs",
"transforms": []
}],
["@babel/plugin-proposal-decorators", { "version": "legacy" }],
"@babel/plugin-proposal-class-properties"
]
}
94 changes: 94 additions & 0 deletions extensibility/ember-storage-batch/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{
"name": "ember-storage-batch",
"version": "0.0.0",
"description": "An extension for ember-storage that adds batch operations.",
"keywords": [
"ember-addon"
],
"repository": "https://github.com/chancancode/ember-polaris-service",
"license": "MIT",
"exports": {
".": {
"types": "./declarations/index.d.ts",
"default": "./dist/index.js"
},
"./addon-main.js": "./addon-main.cjs"
},
"typesVersions": {
"*": {
"*": [
"declarations/*"
]
}
},
"files": [
"addon-main.cjs",
"declarations",
"dist"
],
"scripts": {
"build": "concurrently 'npm:build:*'",
"build:js": "rollup --config",
"build:types": "glint --declaration",
"lint": "concurrently 'npm:lint:*(!fix)' --names 'lint:'",
"lint:fix": "concurrently 'npm:lint:*:fix' --names 'fix:'",
"lint:hbs": "ember-template-lint . --no-error-on-unmatched-pattern",
"lint:hbs:fix": "ember-template-lint . --fix --no-error-on-unmatched-pattern",
"lint:js": "eslint . --cache",
"lint:js:fix": "eslint . --fix",
"lint:types": "glint",
"prepack": "rollup --config",
"start": "concurrently 'npm:start:*'",
"start:js": "rollup --config --watch --no-watch.clearScreen",
"start:types": "glint --declaration --watch",
"test": "echo 'A v2 addon does not have tests, run tests in test-app'"
},
"dependencies": {
"@embroider/addon-shim": "^1.0.0"
},
"peerDependencies": {
"ember-storage": "*"
},
"devDependencies": {
"@babel/core": "^7.17.0",
"@babel/plugin-proposal-class-properties": "^7.16.7",
"@babel/plugin-proposal-decorators": "^7.20.13",
"@babel/plugin-transform-class-static-block": "^7.20.0",
"@babel/plugin-transform-typescript": "^7.22.15",
"@babel/runtime": "^7.17.0",
"@embroider/addon-dev": "^4.1.0",
"@glint/core": "*",
"@glint/environment-ember-loose": "*",
"@glint/environment-ember-template-imports": "*",
"@glint/template": "*",
"@rollup/plugin-babel": "^6.0.3",
"@tsconfig/ember": "^2.0.0",
"@typescript-eslint/eslint-plugin": "^6.7.2",
"@typescript-eslint/parser": "^6.7.2",
"babel-plugin-ember-template-compilation": "^2.2.0",
"concurrently": "^8.0.1",
"ember-template-imports": "*",
"ember-template-lint": "^5.11.2",
"eslint": "^8.33.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-ember": "^11.11.1",
"eslint-plugin-n": "^16.0.0",
"eslint-plugin-prettier": "^5.0.0",
"prettier": "*",
"prettier-plugin-ember-template-tag": "*",
"rollup": "^3.21.8",
"typescript": "*"
},
"publishConfig": {
"registry": "https://registry.npmjs.org"
},
"ember": {
"edition": "octane"
},
"ember-addon": {
"version": 2,
"type": "addon",
"main": "addon-main.cjs",
"app-js": {}
}
}
63 changes: 63 additions & 0 deletions extensibility/ember-storage-batch/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { babel } from '@rollup/plugin-babel';
import { Addon } from '@embroider/addon-dev/rollup';

const addon = new Addon({
srcDir: 'src',
destDir: 'dist',
});

export default {
// This provides defaults that work well alongside `publicEntrypoints` below.
// You can augment this if you need to.
output: addon.output(),

plugins: [
// These are the modules that users should be able to import from your
// addon. Anything not listed here may get optimized away.
// By default all your JavaScript modules (**/*.js) will be importable.
// But you are encouraged to tweak this to only cover the modules that make
// up your addon's public API. Also make sure your package.json#exports
// is aligned to the config here.
// See https://github.com/embroider-build/embroider/blob/main/docs/v2-faq.md#how-can-i-define-the-public-exports-of-my-addon
addon.publicEntrypoints(['**/*.js', 'index.js', 'template-registry.js']),

// These are the modules that should get reexported into the traditional
// "app" tree. Things in here should also be in publicEntrypoints above, but
// not everything in publicEntrypoints necessarily needs to go here.
addon.appReexports([
'components/**/*.js',
'helpers/**/*.js',
'modifiers/**/*.js',
'services/**/*.js',
]),

// Follow the V2 Addon rules about dependencies. Your code can import from
// `dependencies` and `peerDependencies` as well as standard Ember-provided
// package names.
addon.dependencies(),

// This babel config should *not* apply presets or compile away ES modules.
// It exists only to provide development niceties for you, like automatic
// template colocation.
//
// By default, this will load the actual babel config from the file
// babel.config.json.
babel({
extensions: ['.js', '.gjs', '.ts', '.gts'],
babelHelpers: 'bundled',
}),

// Ensure that standalone .hbs files are properly integrated as Javascript.
addon.hbs(),

// Ensure that .gjs files are properly integrated as Javascript
addon.gjs(),

// addons are allowed to contain imports of .css files, which we want rollup
// to leave alone and keep in the published output.
addon.keepAssets(['**/*.css']),

// Remove leftover build artifacts when starting a new build.
addon.clean(),
],
};
Loading