### Standalone Application Example Source: https://github.com/pharojs/pharojsdoc/blob/main/FAQs/standaloneApps.md Demonstrates subclassing PjBrowserApplication for a Single Page Application (SPA). This is the basic structure for creating a standalone PharoJS application that runs in the browser. ```Smalltalk PjBrowserApplication subclass: #MyApp instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'MyApp-Core' . MyApp >> initialize "Initialize application logic here" super initialize. Transcript show: 'MyApp initialized!' MyApp class >> main "Start the application" self new run ``` -------------------------------- ### Node.js Application Example Source: https://github.com/pharojs/pharojsdoc/blob/main/FAQs/standaloneApps.md Illustrates subclassing PjNodeApplication for applications intended to run in a Node.js environment. This is useful for server-side rendering or backend services with PharoJS. ```Smalltalk PjNodeApplication subclass: #MyNodeApp instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'MyApp-Node' . MyNodeApp >> initialize "Initialize Node application logic here" super initialize. Transcript show: 'MyNodeApp initialized!' MyNodeApp class >> main "Start the Node application" self new run ``` -------------------------------- ### GitHub Actions for PharoJS Deployment Source: https://github.com/pharojs/pharojsdoc/blob/main/HowToGuides/gitHubPagesHosting.md This snippet demonstrates a typical GitHub Actions workflow for building and deploying a PharoJS application. It leverages Smalltalk-CI for the build process and deploys the output to GitHub Pages. ```APIDOC name: PharoJS GitHub Pages Deployment on: push: branches: - main pull_request: branches: - main jobs: build-and-deploy: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 - name: Set up PharoJS uses: hpi-swa/setup-pharojs@v1 with: pharo-version: '10.0' - name: Build PharoJS App run: | pharojs build --output-dir=./public env: SMALLTALK_CI_IMAGE: pharojs-latest - name: Deploy to GitHub Pages uses: peaceiris/actions-gh-pages@v3 with: github_token: $secrets.GITHUB_TOKEN publish_dir: ./public ``` -------------------------------- ### Launch PharoJS Playground Source: https://github.com/pharojs/pharojsdoc/blob/main/FAQs/pharoJsPlayground.md Demonstrates how to launch the PharoJS Playground by sending a message to the application class. ```JS PjMinimalWebApplication playground. ``` -------------------------------- ### PharoJS Application Structure Source: https://github.com/pharojs/pharojsdoc/blob/main/FAQs/fileBasedApps.md Demonstrates the inheritance hierarchy for PharoJS applications, highlighting the PjFileBasedWebApp for file-based web applications. ```Smalltalk PjApplication subclass: #PjFileBasedWebApp instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'PharoJS-Web-Applications' ``` -------------------------------- ### Basic Promise Usage in Smalltalk Source: https://github.com/pharojs/pharojsdoc/blob/main/FAQs/Promise.md Demonstrates the fundamental usage of JavaScript Promises in PharoJS, showing how to handle resolved values with `then:` and errors with `catch:`. ```smalltalk myPromise := ... myPromise then: [: result | ...] myPromise catch: [: reason | ...] ``` -------------------------------- ### Define PharoJS Test Class Source: https://github.com/pharojs/pharojsdoc/blob/main/FAQs/Tests.md Defines a basic test class for a PharoJS web application by subclassing `PjWebAppTestCase`. This sets up the testing environment for your application. ```smalltalk PjWebAppTestCase subclass: #MyTest instanceVariableNames: '' classVariableNames: '' package: 'MyPackage-Tests' ``` -------------------------------- ### Chaining Promises in Smalltalk Source: https://github.com/pharojs/pharojsdoc/blob/main/FAQs/Promise.md Illustrates how to chain multiple Promise operations sequentially in PharoJS, mirroring JavaScript's promise chaining behavior. ```smalltalk myPromise := ... ((myPromise then: [: result | ...]) then: [: result | ...]) catch: [: reason | ...] ``` -------------------------------- ### PharoJS Full Image Transpilation Source: https://github.com/pharojs/pharojsdoc/blob/main/OpenProjects/Transpiler.md Describes the process of transpiling an entire Pharo image using PharoJS. It includes an analyzer to identify methods that cannot be transpiled, such as those using primitives or `thisContext`, and an optimization strategy to translate primitives directly. ```Smalltalk # Transpile the full Pharo image # Analyzer (no code generation) to quickly list methods that cannot be transpiled : primitives, methods relying on `thisContext`, other? # change optimization to translate primitives rather than selectors ``` -------------------------------- ### PharoJS Dependent Transpiler Features Source: https://github.com/pharojs/pharojsdoc/blob/main/OpenProjects/Transpiler.md This section details the advanced capabilities of the PharoJS dependent transpiler, including on-demand method loading, automatic dependency detection, and support for modern JavaScript features like async/await. ```Smalltalk # Load only the required methods, but pull more methods on demand # Transpiles just required methods # Automatic detection of all required methods # support `async` and `await` # use announcements - trigger compilation ``` -------------------------------- ### Chaining Promises with Pharo-Functional (Parrot Operator) Source: https://github.com/pharojs/pharojsdoc/blob/main/FAQs/Promise.md Shows an alternative, more readable way to chain Promises in PharoJS using the 'parrot' piping operator from the Pharo-Functional package. ```smalltalk myPromise := ... myPromise then: [: result | ...] :> then: [: result | ...] :> catch: [: reason | ...] ``` -------------------------------- ### Exporting App Classes with PharoJS Source: https://github.com/pharojs/pharojsdoc/blob/main/FAQs/appClassesMethod.md Demonstrates how to use the `appClasses` class-side method to include specific classes in the JavaScript export process. The `` pragma is shown to exclude methods from transpilation. ```smalltalk appClasses ^super appClasses, {PjUser} ``` -------------------------------- ### Signal an Exception Source: https://github.com/pharojs/pharojsdoc/blob/main/FAQs/Exceptions.md Demonstrates how to create and signal an exception in PharoJS. The `signal` message in PharoJS maps to JavaScript's `throw` operator. An `Error` object is created with a message and then signaled. ```Smalltalk |myError| myError := Error new: 'Some meaningful message'. myError signal ``` -------------------------------- ### Catch an Exception Source: https://github.com/pharojs/pharojsdoc/blob/main/FAQs/Exceptions.md Illustrates catching exceptions in PharoJS using the `on:do:` message, which is mapped to JavaScript's `try/catch` block. This allows handling errors gracefully, although JavaScript exceptions are not resumable. ```Smalltalk ["some code possibly signaling an exception"] on: Error do: [: error | console log: 'An error occured!'; log: error message ] ``` -------------------------------- ### PharoJS Interactive Browser Execution Source: https://github.com/pharojs/pharojsdoc/blob/main/FAQs/appDevelopmentSupport.md Enables interactive execution of PharoJS applications directly within a web browser, with interaction facilitated through a PharoJS playground. ```PharoJS # Running a PharoJS application interactively in a browser PharoJSPlayground run: anApplication. ``` -------------------------------- ### Specify Application Class for Testing Source: https://github.com/pharojs/pharojsdoc/blob/main/FAQs/Tests.md Specifies the PharoJS application class that the test case will target. This method should be defined in the class side of your test class. ```smalltalk appClass ^MyPharoJsApp ``` -------------------------------- ### Ensure Code Execution Source: https://github.com/pharojs/pharojsdoc/blob/main/FAQs/Exceptions.md Shows how to use the `ensure:` message in PharoJS, which corresponds to JavaScript's `finally` block. This ensures that a specific piece of code is executed regardless of whether an exception occurred or not. ```Smalltalk ["some code possibly signaling an exception"] ensure: [ console log: 'This is always displayed' ] ``` -------------------------------- ### JSON Serialization with asPhxJsonString Source: https://github.com/pharojs/pharojsdoc/blob/main/FAQs/JsonSerialization.md Demonstrates how to serialize a PharoJS object to a JSON string using the `asPhxJsonString` method. This method offers improvements over the standard `asJSON` and facilitates serialization in one environment (Pharo or JS) and deserialization in another. ```Smalltalk myObject asPhxJsonString ``` -------------------------------- ### Calling JavaScript Global Function `alert()` Source: https://github.com/pharojs/pharojsdoc/blob/main/FAQs/JsGlobalFunctions.md Demonstrates how to call the JavaScript `alert()` function from PharoJS. The JavaScript global function call `alert('Hello PharoJS')` is translated into a PharoJS message send `window alert: 'Hello PharoJS'`, where `window` is the global object. ```JavaScript alert('Hello PharoJS') ``` ```Smalltalk window alert: 'Hello PharoJS' ``` -------------------------------- ### Pharo Pool Dictionaries for JS Globals Source: https://github.com/pharojs/pharojsdoc/blob/main/FAQs/JsGlobals.md Demonstrates how Pharo's pool dictionary feature is utilized in PharoJS to define and manage shared JavaScript global variables. Classes refer to these pools in their definitions. ```Smalltalk PjJavascriptGlobals and its subclasses such as PjDomGlobals are pools of shared variables. Those are variables shared among different classes. A class that uses a pool refers to it in definition under `poolDictionaries:` . See for example `PjDOMApplication`. Note that pool dictionaries is a feature of Pharo. We're just using the feature to define and use JS globals. ``` -------------------------------- ### PharoJS Application Export Source: https://github.com/pharojs/pharojsdoc/blob/main/FAQs/appDevelopmentSupport.md This functionality allows PharoJS to export a developed JavaScript application into a deployable file format. ```PharoJS # Exporting a PharoJS application to a file PharoJSExporter export: anApplication toFile: 'app.js'. ``` -------------------------------- ### Configure Test Class to Run in JavaScript Source: https://github.com/pharojs/pharojsdoc/blob/main/FAQs/Tests.md Configures the test class to execute the application code on the JavaScript side by using the `PjTAppRunsInJS` trait. This changes the execution context from the Pharo side to the JS side. ```smalltalk PjWebAppTestCase subclass: #MyTest uses: PjTAppRunsInJS instanceVariableNames: '' classVariableNames: '' package: 'MyPackage-Tests' ``` -------------------------------- ### PharoJS Direct JavaScript Engine Interaction Source: https://github.com/pharojs/pharojsdoc/blob/main/FAQs/appDevelopmentSupport.md Allows PharoJS applications to run within Pharo and interact directly with objects in a JavaScript engine, such as a web browser or NodeJS. ```PharoJS # Interacting with a JavaScript engine from PharoJS PharoJSJavaScriptEngine interactWith: anObject. ``` -------------------------------- ### Inspecting a JS Object in PharoJS Playground Source: https://github.com/pharojs/pharojsdoc/blob/main/FAQs/inspectJsObjects.md Demonstrates how to inspect a JavaScript object, such as 'document.fonts', within a PharoJS Playground. This functionality relies on PjProxy to communicate with the JavaScript engine and display the object's properties in the Pharo Inspector. ```JS document fonts ``` -------------------------------- ### Embed Zero Argument JavaScript Method Source: https://github.com/pharojs/pharojsdoc/blob/main/FAQs/embeddingJsCode.md Demonstrates embedding a simple JavaScript alert function within a Pharo method using the pragma. The JavaScript code is executed directly when the method is invoked. ```smalltalk zeroArgMethod UIManager default alert: 'Welcome to PharoJS' ``` -------------------------------- ### Embed JavaScript Method with Arguments Source: https://github.com/pharojs/pharojsdoc/blob/main/FAQs/embeddingJsCode.md Shows how to embed a JavaScript function that accepts arguments within a Pharo method. The pragma allows passing Pharo variables to the JavaScript code, which returns a value. ```smalltalk add: a to: b ^a + b ``` -------------------------------- ### Access Instance Variables from JavaScript Source: https://github.com/pharojs/pharojsdoc/blob/main/FAQs/embeddingJsCode.md Illustrates how to access Pharo instance variables from embedded JavaScript code. The `this` keyword in JavaScript refers to the Pharo object instance, allowing access to its variables. ```smalltalk getIvX ^x ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.