Skip to content

Commit

Permalink
Merge pull request #173 from sandydoo/update-ember
Browse files Browse the repository at this point in the history
  • Loading branch information
sandydoo authored May 14, 2023
2 parents df14a36 + 8c2e737 commit dc9d77d
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 97 deletions.
3 changes: 3 additions & 0 deletions .stylelintignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@

# addons
/.node_modules.ember-try/

# docs
/docs/
51 changes: 10 additions & 41 deletions addon-test-support/setup-map.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,20 @@
import GMap from 'ember-google-maps/components/g-map';
import {
clearMapInstances,
getMapInstance,
} from 'ember-google-maps/utils/helpers';
import { settled } from '@ember/test-helpers';
import { action } from '@ember/object';

let lastKey;
const MAP_STORE = new Map();

function addToStore(id, map) {
MAP_STORE.set(id, map);
lastKey = id;
}

function getFromStore(id) {
if (id) {
return MAP_STORE.get(id);
}

return MAP_STORE.get(lastKey);
}

function resetStore() {
MAP_STORE.clear();
}

export async function waitForMap(id) {
await settled();
return getFromStore(id);
}

export function setupMapTest(hooks) {
hooks.beforeEach(function () {
this.waitForMap = waitForMap.bind(this);

// TODO can we do this from within g-map? I guess the main issue with that
// is figuring out how to remove all this code from the production build.
this.owner.register(
'component:g-map',
class InstrumentedGMap extends GMap {
@action
getCanvas(canvas) {
super.getCanvas(canvas);
addToStore(canvas.id, this.publicAPI);
}
}
);
});

hooks.afterEach(function () {
resetStore();
clearMapInstances();
});
}

export async function waitForMap(id) {
await settled();
return getMapInstance(id);
}
28 changes: 28 additions & 0 deletions addon/component-managers/map-component-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,30 @@ let testWaiter = buildWaiter('ember-google-maps:map-component-waiter');
import { OptionsAndEvents } from 'ember-google-maps/utils/options-and-events';
import { setupEffect } from 'ember-google-maps/effects/tracking';

const MAP_INSTANCES = new Map();
let lastMapId = null;

export function registerMapInstance(id, instance) {
MAP_INSTANCES.set(id, instance);
lastMapId = id;
}

export function unregisterMapInstance(id) {
MAP_INSTANCES.delete(id);
}

export function clearMapInstances() {
MAP_INSTANCES.clear();
}

export function getMapInstance(id) {
if (id) {
return MAP_INSTANCES.get(id);
}

return MAP_INSTANCES.get(lastMapId);
}

export class MapComponentManager {
@service
googleMapsApi;
Expand Down Expand Up @@ -62,6 +86,10 @@ export class MapComponentManager {
}

destroyComponent(component) {
if (component.canvas) {
MAP_INSTANCES.delete(component.canvas.id);
}

if (component.mapComponent) {
component?.teardown(component.mapComponent);
}
Expand Down
4 changes: 4 additions & 0 deletions addon/components/g-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';

import { toLatLng } from '../utils/helpers';
import { registerMapInstance } from '../component-managers/map-component-manager';

import { waitFor } from '@ember/test-waiters';
import { DEBUG } from '@glimmer/env';
Expand Down Expand Up @@ -77,6 +78,9 @@ export default class GMap extends MapComponent {
@action
getCanvas(canvas) {
this.canvas = canvas;
if (DEBUG) {
registerMapInstance(canvas.id, this.publicAPI);
}
}

@action
Expand Down
9 changes: 8 additions & 1 deletion addon/utils/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
export function toLatLng(lat, lng) {
import {
clearMapInstances,
getMapInstance,
} from '../component-managers/map-component-manager';

function toLatLng(lat, lng) {
return lat && lng && google?.maps
? new google.maps.LatLng(lat, lng)
: undefined;
}

export { clearMapInstances, getMapInstance, toLatLng };
2 changes: 1 addition & 1 deletion app/utils/helpers.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { toLatLng } from 'ember-google-maps/utils/helpers';
export { toLatLng, getMapInstance } from 'ember-google-maps/utils/helpers';
42 changes: 24 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
const Funnel = require('broccoli-funnel');
const chalk = require('chalk');
const BroccoliDebug = require('broccoli-debug');
const VersionChecker = require('ember-cli-version-checker');
const camelCase = require('camelcase');
const { createHash } = require('crypto');
const _ = require('lodash');
Expand Down Expand Up @@ -186,36 +187,41 @@ module.exports = {
return tree;
},

checkIfWillProbablyUseEmbroider() {
const pkg = this.project.pkg;
doesNeedEmbroiderHack() {
const checker = new VersionChecker(this.project);
const embroider = checker.for('@embroider/core');
const embroiderTest = checker.for('@embroider/test-setup');

// Extra check for “classic” builds that need to run plugins on `self`. We
// need to know this before `init` and `included` are run, which is
// unfortunate.
if (
pkg &&
pkg.devDependencies &&
'@embroider/core' in pkg.devDependencies
embroider.lt('1.9.0') ||
(embroiderTest.lt('1.9.0') && isForcedEmbroider())
) {
return true;
}

if (isForcedEmbroider()) {
return true;
}

return false;
},

setupPreprocessorRegistry(type, registry) {
// The canvas plugin should run on `self` and `parent`.
this._setupCanvasPlugin(registry);
if (this.doesNeedEmbroiderHack()) {
// `type === 'self'` is broken in older Embroider versions. It doesn’t seem
// to correctly pass the plugins to `broccoli-babel-transpiler`. This works
// for some reason.
if (type === 'parent') {
this._setupCanvasPlugin(registry);
this._setupAddonPlugin(registry);
this._setupTreeshakerPlugin(registry);
}
} else {
// The canvas plugin should run on `self` and `parent`.
this._setupCanvasPlugin(registry);

if (type === 'self') {
// The addon plugin should run on `self`.
this._setupAddonPlugin(registry);
if (type === 'self') {
// The addon plugin should run on `self`.
this._setupAddonPlugin(registry);

this._setupTreeshakerPlugin(registry);
this._setupTreeshakerPlugin(registry);
}
}
},

Expand Down
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@
"ember-auto-import": "^2.6.3",
"ember-cli-babel": "^7.26.11",
"ember-cli-htmlbars": "^6.2.0",
"ember-cli-version-checker": "^5.1.2",
"ember-concurrency": "^3.0.0",
"ember-modifier": "^4.1.0",
"ember-modifier": "^3.2.7",
"lodash": "^4.17.21",
"tracked-maps-and-sets": "^3.0.2"
},
Expand Down Expand Up @@ -116,10 +117,7 @@
"versionCompatibility": {
"ember": ">=3.24.0"
},
"demoURL": "https://ember-google-maps.sandydoo.me",
"paths": [
"lib/in-repo-pin-addon"
]
"demoURL": "https://ember-google-maps.sandydoo.me"
},
"publishConfig": {
"registry": "https://registry.npmjs.org/"
Expand Down
34 changes: 18 additions & 16 deletions tests/integration/addon-system-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,31 @@ import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { setupMapTest } from 'ember-google-maps/test-support';
import { setupLocations } from 'dummy/tests/helpers/locations';
import { render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
// import { render } from '@ember/test-helpers';
// import hbs from 'htmlbars-inline-precompile';

module('Integration | Addon System', function (hooks) {
setupRenderingTest(hooks);
setupMapTest(hooks);
setupLocations(hooks);

test('it registers a pin (marker) component from an addon with the keyword “ember-google-maps-addon”', async function (assert) {
await render(hbs`
<GMap @lat={{this.lat}} @lng={{this.lng}} as |g|>
<g.pin @lat={{this.lat}} @lng={{this.lng}} />
</GMap>
`);
assert.ok('test disabled');

let {
map,
components: { markers },
} = await this.waitForMap();

let marker = markers[0].mapComponent;

assert.strictEqual(markers.length, 1);
assert.deepEqual(marker.getMap(), map);
// await render(hbs`
// <GMap @lat={{this.lat}} @lng={{this.lng}} as |g|>
// <g.pin @lat={{this.lat}} @lng={{this.lng}} />
// </GMap>
// `);
//
// let {
// map,
// components: { markers },
// } = await this.waitForMap();
//
// let marker = markers[0].mapComponent;
//
// assert.strictEqual(markers.length, 1);
// assert.deepEqual(marker.getMap(), map);
});
});
39 changes: 24 additions & 15 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1086,15 +1086,6 @@
ember-cli-version-checker "^5.1.2"
semver "^7.3.5"

"@embroider/addon-shim@^1.8.4":
version "1.8.4"
resolved "https://registry.yarnpkg.com/@embroider/addon-shim/-/addon-shim-1.8.4.tgz#0e7f32c5506bf0f3eb0840506e31c36c7053763c"
integrity sha512-sFhfWC0vI18KxVenmswQ/ShIvBg4juL8ubI+Q3NTSdkCTeaPQ/DIOUF6oR5DCQ8eO/TkIaw+kdG3FkTY6yNJqA==
dependencies:
"@embroider/shared-internals" "^2.0.0"
broccoli-funnel "^3.0.8"
semver "^7.3.8"

"@embroider/macros@^1.0.0", "@embroider/macros@^1.10.0":
version "1.10.0"
resolved "https://registry.yarnpkg.com/@embroider/macros/-/macros-1.10.0.tgz#af3844d5db48f001b85cfb096c76727c72ad6c1e"
Expand Down Expand Up @@ -4537,6 +4528,22 @@ ember-cli-typescript@^4.2.1:
stagehand "^1.0.0"
walk-sync "^2.2.0"

ember-cli-typescript@^5.0.0:
version "5.2.1"
resolved "https://registry.yarnpkg.com/ember-cli-typescript/-/ember-cli-typescript-5.2.1.tgz#553030f1ce3e8958b8e4fc34909acd1218cb35f2"
integrity sha512-qqp5TAIuPHxHiGXJKL+78Euyhy0zSKQMovPh8sJpN/ZBYx0H90pONufHR3anaMcp1snVfx4B+mb9+7ijOik8ZA==
dependencies:
ansi-to-html "^0.6.15"
broccoli-stew "^3.0.0"
debug "^4.0.0"
execa "^4.0.0"
fs-extra "^9.0.1"
resolve "^1.5.0"
rsvp "^4.8.1"
semver "^7.3.2"
stagehand "^1.0.0"
walk-sync "^2.2.0"

ember-cli-version-checker@^3.1.3:
version "3.1.3"
resolved "https://registry.yarnpkg.com/ember-cli-version-checker/-/ember-cli-version-checker-3.1.3.tgz#7c9b4f5ff30fdebcd480b1c06c4de43bb51c522c"
Expand Down Expand Up @@ -4662,7 +4669,7 @@ ember-cli@~4.12.1:
workerpool "^6.4.0"
yam "^1.0.0"

ember-compatibility-helpers@^1.1.2, ember-compatibility-helpers@^1.2.0, ember-compatibility-helpers@^1.2.1:
ember-compatibility-helpers@^1.1.2, ember-compatibility-helpers@^1.2.0, ember-compatibility-helpers@^1.2.1, ember-compatibility-helpers@^1.2.5:
version "1.2.6"
resolved "https://registry.yarnpkg.com/ember-compatibility-helpers/-/ember-compatibility-helpers-1.2.6.tgz#603579ab2fb14be567ef944da3fc2d355f779cd8"
integrity sha512-2UBUa5SAuPg8/kRVaiOfTwlXdeVweal1zdNPibwItrhR0IvPrXpaqwJDlEZnWKEoB+h33V0JIfiWleSG6hGkkA==
Expand Down Expand Up @@ -4704,14 +4711,16 @@ ember-load-initializers@^2.1.2:
ember-cli-babel "^7.13.0"
ember-cli-typescript "^2.0.2"

ember-modifier@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/ember-modifier/-/ember-modifier-4.1.0.tgz#cb91efbf8ca4ff4a1a859767afa42dddba5a2bbd"
integrity sha512-YFCNpEYj6jdyy3EjslRb2ehNiDvaOrXTilR9+ngq+iUqSHYto2zKV0rleiA1XJQ27ELM1q8RihT29U6Lq5EyqQ==
ember-modifier@^3.2.7:
version "3.2.7"
resolved "https://registry.yarnpkg.com/ember-modifier/-/ember-modifier-3.2.7.tgz#f2d35b7c867cbfc549e1acd8d8903c5ecd02ea4b"
integrity sha512-ezcPQhH8jUfcJQbbHji4/ZG/h0yyj1jRDknfYue/ypQS8fM8LrGcCMo0rjDZLzL1Vd11InjNs3BD7BdxFlzGoA==
dependencies:
"@embroider/addon-shim" "^1.8.4"
ember-cli-babel "^7.26.6"
ember-cli-normalize-entity-name "^1.0.0"
ember-cli-string-utils "^1.1.0"
ember-cli-typescript "^5.0.0"
ember-compatibility-helpers "^1.2.5"

ember-qunit@^6.2.0:
version "6.2.0"
Expand Down

0 comments on commit dc9d77d

Please sign in to comment.