Skip to content

Commit

Permalink
make some api private
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed Oct 15, 2022
1 parent d8b6007 commit 888f859
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 90 deletions.
1 change: 1 addition & 0 deletions packages/tracking/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"@babel/runtime": "^7.19.4",
"@rollup/plugin-babel": "^6.0.0",
"@rollup/plugin-typescript": "^9.0.1",
"rollup-plugin-ts": "^3.0.2",
"tslib": "^2.4.0",
"walk-sync": "^3.0.0",
"typescript": "^4.8.4"
Expand Down
25 changes: 20 additions & 5 deletions packages/tracking/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Addon } from '@embroider/addon-dev/rollup';
import babel from '@rollup/plugin-babel';
import ts from 'rollup-plugin-ts';

const addon = new Addon({
srcDir: 'src',
Expand All @@ -16,11 +16,26 @@ export default {
plugins: [
// These are the modules that users should be able to import from your
// addon. Anything not listed here may get optimized away.
addon.publicEntrypoints(['index.js']),
addon.publicEntrypoints(['index.js', '-private.js']),

babel({
extensions: ['.js', '.ts'],
babelHelpers: 'runtime', // we should consider "external",
ts({
// can be changed to swc or other transpilers later
// but we need the ember plugins converted first
// (template compilation and co-location)
transpiler: 'babel',
browserslist: ['last 2 firefox versions', 'last 2 chrome versions', 'last 2 safari versions'],
tsconfig: {
fileName: '../../tsconfig.json',
hook: (config) => ({
...config,
declaration: true,
declarationMap: true,
// See: https://devblogs.microsoft.com/typescript/announcing-typescript-4-5/#beta-delta
// Allows us to use `exports` to define types per export
// However, we can't use that feature until the minimum supported TS is 4.7+
declarationDir: './dist',
}),
},
}),

// Remove leftover build artifacts when starting a new build.
Expand Down
77 changes: 77 additions & 0 deletions packages/tracking/src/-private.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
type OpaqueFn = (...args: unknown[]) => unknown;
type Tag = { ref: null };
type Transaction = {
cbs: Set<OpaqueFn>;
props: Set<Tag>;
sub: Set<Tag>;
parent: Transaction | null;
};
let TRANSACTION: Transaction | null = null;

function createTransaction() {
let transaction: Transaction = {
cbs: new Set(),
props: new Set(),
sub: new Set(),
parent: null,
};
if (TRANSACTION) {
transaction.parent = TRANSACTION;
}
TRANSACTION = transaction;
}

export function subscribe(obj: Tag): void {
if (TRANSACTION) {
TRANSACTION.sub.add(obj);
} else {
obj.ref;
}
}

function flushTransaction() {
let transaction = TRANSACTION!;
TRANSACTION = transaction.parent;
transaction.cbs.forEach((cb) => {
cb();
});
transaction.props.forEach((obj: Tag) => {
obj.ref = null;
});
transaction.sub.forEach((obj: Tag) => {
if (!transaction.props.has(obj)) {
obj.ref;
}
});
}

export function addToTransaction(obj: Tag): void {
if (TRANSACTION) {
TRANSACTION.props.add(obj);
} else {
obj.ref = null;
}
}
export function addTransactionCB(method: OpaqueFn): void {
if (TRANSACTION) {
TRANSACTION.cbs.add(method);
} else {
method();
}
}

export function transact<T extends OpaqueFn>(method: T): ReturnType<T> {
createTransaction();
const ret = method();
flushTransaction();
return ret as ReturnType<T>;
}

export function memoTransact<T extends OpaqueFn>(method: T): (...args: unknown[]) => ReturnType<T> {
return function (...args: unknown[]) {
createTransaction();
const ret = method(...args);
flushTransaction();
return ret as ReturnType<T>;
};
}
78 changes: 1 addition & 77 deletions packages/tracking/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1 @@
type OpaqueFn = (...args: unknown[]) => unknown;
type Tag = { ref: null };
type Transaction = {
cbs: Set<OpaqueFn>;
props: Set<Tag>;
sub: Set<Tag>;
parent: Transaction | null;
};
let TRANSACTION: Transaction | null = null;

function createTransaction() {
let transaction: Transaction = {
cbs: new Set(),
props: new Set(),
sub: new Set(),
parent: null,
};
if (TRANSACTION) {
transaction.parent = TRANSACTION;
}
TRANSACTION = transaction;
}

export function subscribe(obj: Tag): void {
if (TRANSACTION) {
TRANSACTION.sub.add(obj);
} else {
obj.ref;
}
}

function flushTransaction() {
let transaction = TRANSACTION!;
TRANSACTION = transaction.parent;
transaction.cbs.forEach((cb) => {
cb();
});
transaction.props.forEach((obj: Tag) => {
obj.ref = null;
});
transaction.sub.forEach((obj: Tag) => {
if (!transaction.props.has(obj)) {
obj.ref;
}
});
}

export function addToTransaction(obj: Tag): void {
if (TRANSACTION) {
TRANSACTION.props.add(obj);
} else {
obj.ref = null;
}
}
export function addTransactionCB(method: OpaqueFn): void {
if (TRANSACTION) {
TRANSACTION.cbs.add(method);
} else {
method();
}
}

export function transact<T extends OpaqueFn>(method: T): ReturnType<T> {
createTransaction();
const ret = method();
flushTransaction();
return ret as ReturnType<T>;
}

export function memoTransact<T extends OpaqueFn>(method: T): (...args: unknown[]) => ReturnType<T> {
return function (...args: unknown[]) {
createTransaction();
const ret = method(...args);
flushTransaction();
return ret as ReturnType<T>;
};
}
export { transact, memoTransact } from './-private';

0 comments on commit 888f859

Please sign in to comment.